  
  (function() { // Overriding XMLHttpRequest
    
    var oldXHR = window.XMLHttpRequest;
      function newXHR() {
        var realXHR = new oldXHR();
        realXHR.addEventListener("readystatechange", function() { 
            if (realXHR.readyState == 4) {
                if (realXHR.status == 200) {
                    $("#spinner").hide();
                    $("#instructions").show();
                }
              }
        }, false);
        return realXHR;
      }
      window.XMLHttpRequest = newXHR;
  })();

  function get(name) {
    if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(location.search)) {
      return decodeURIComponent(name[1]);
    }
  }

  function getChart() {
    if ($("#sym_input").val() !== undefined) {
      
      $.ajax({
        url: "data",
        context: document.body,
        data: "ticker=" + $("#sym_input").val().toUpperCase(),
        statusCode: {
          204: function() {
            $("#company").text("NOT FOUND");
            $("#graphdiv").hide();
          },
          200: function(data) {
            $("#company").text(data);
            $("#instructions").hide();
            sym = $("#sym_input").val();
            years = $("#years_input").val();
              
            dateString = (new Date()).toISOString().substring(0,10);
            $("#spinner").show();
            g = new Dygraph(document.getElementById("graphdiv"), 
                sym + "-" + dateString + "-" + years + ".csv",
           //   "psedata.txt", 
              {
                pixelsPerXLabel: 40
              }
             );
          } //200
        } //statusCode
      }); //ajax
    } //undefined
  }
  
  function getCandleData(ticker) {
	    if (ticker !== undefined) {
	      
	      $.ajax({
	        url: "data",
	        dataType: 'text',
	        context: document.body,
	        data: "candle=" + ticker,
	        statusCode: {
	          204: function() {
	            $("#company").text("NOT FOUND");
	            $("#graphdiv").hide();
	          },
	          200: function(data) {
	        	  var data2 = eval(data);
	        	  plot2 = $.jqplot('chart2',[data2],{
	        		  title: ticker,
	        		  series: [{renderer:$.jqplot.OHLCRenderer, rendererOptions:{candleStick:true}}],
	        		  axesDefaults:{},
	        		  axes: {
	        		      xaxis: {
	        		          renderer:$.jqplot.DateAxisRenderer,
	        		          tickOptions:{formatString:'%Y.%m.%d'}
	        		      },
	        		      yaxis: {
	        		          tickOptions:{formatString:'$%.2f'}
	        		      }
	        		  },
	        		  highlighter: {
	        		      showMarker:true,
	        		      tooltipAxes: 'y',
	        		      tooltipLocation: 'nw',
	        		      yvalues: 4,
	        		      formatString:'<table class="jqplot-highlighter"><tr><td>open:</td><td>%s</td></tr><tr><td>hi:</td><td>%s</td></tr><tr><td>low:</td><td>%s</td></tr><tr><td>close:</td><td>%s</td></tr></table>'
	        		  }
	        		});
	          } //200
	        } //statusCode
	      }); //ajax
	    } //undefined
	  }

  
